<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/19
 * Time: 18:49
 */
class 递归创建多级目录的通用方法
{
    /**
     *
    +--------------------------------------------------------------------
     * Description 递归创建目录
    +--------------------------------------------------------------------
     * @param  string $dir 需要创新的目录
    +--------------------------------------------------------------------
     * @return 若目录存在,或创建成功则返回为TRUE
    +--------------------------------------------------------------------
     * @author gongwen
    +--------------------------------------------------------------------
     */
    function mkdirs($dir, $mode = 0777){
        if (is_dir($dir) || mkdir($dir, $mode)) return TRUE;
        if (!mkdirs(dirname($dir), $mode)) return FALSE;
        return mkdir($dir, $mode);
    }

    /**
     *
    +--------------------------------------------------------------------
     * Description 递归删除目录
    +--------------------------------------------------------------------
     * @param  string $dir 需要删除的目录
    +--------------------------------------------------------------------
     * @return 若目录不存在或册除成功则返回为TRUE
    +--------------------------------------------------------------------
     * @author gongwen
    +--------------------------------------------------------------------
     */
    function rmdirs($dir){
        if (!is_dir($dir) || rmdir($dir)) return TRUE;
        if($dir_handle=opendir($dir)){
            while($filename=readdir($dir_handle)){
                if($filename!='.' && $filename!='..'){
                    $subFile=$dir.'/'.$filename;
                }
                is_dir($subFile)?rmdirs($subFile):unlink($subFile);
            }
            closedir($dir_handle);
            return rmdir($dir);
        }
    }
}